package cn.wjh.business.service.impl;

import cn.wjh.business.domain.Competition;
import cn.wjh.business.domain.Views;
import cn.wjh.business.domain.Works;
import cn.wjh.business.mapper.ViewsMapper;
import cn.wjh.business.service.ICompetitionService;
import cn.wjh.business.service.IViewsService;
import cn.wjh.business.service.IWorksService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 浏览Service业务层处理
 *
 * @author jiraWu
 * @date 2023-10-19
 */
@Service
@Slf4j
public class ViewsServiceImpl extends ServiceImpl<ViewsMapper, Views> implements IViewsService {
	@Resource
	private ViewsMapper viewsMapper;
	@Resource
	private ICompetitionService competitionService;

	@Resource
	private IWorksService worksService;

	/**
	 * 查询浏览
	 *
	 * @param viewsId 浏览主键
	 * @return 浏览
	 */
	@Override
	public Views selectViewsByViewsId(Long viewsId) {
		return viewsMapper.selectViewsByViewsId(viewsId);
	}

	/**
	 * 查询浏览列表
	 *
	 * @param views 浏览
	 * @return 浏览
	 */
	@Override
	public List<Views> selectViewsList(Views views) {
		List<Views> list = viewsMapper.selectViewsList(views);
		/*获取预览的比赛*/
		if (views.getType().equals("competition")) {
			list.forEach(item -> {
				item.setData(competitionService.selectCompetitionByCompetitionId(item.getDataId()));
			});
		} else {
			/*预览的作品*/
			list.forEach(item -> {
				item.setData(worksService.selectWorksByWorksId(item.getDataId()));
			});
		}
		return list;
	}

	/**
	 * 新增浏览
	 *
	 * @param views 浏览
	 * @return 结果
	 */
	@Override
	@Transactional
	public int insertViews(Views views) {
		/*
		 * 1.登录后(competition/views)
		 *   1.1.已存在(更新浏览时间)
		 * 2.未登录(competition)
		 * */
		LoginUser loginUser = null;
		try {
			loginUser = SecurityUtils.getLoginUser();
			/*设置浏览用户*/
			views.setCreateTime(DateUtils.getNowDate());
			views.setUpdateTime(DateUtils.getNowDate());
			views.setUserId(loginUser.getUserId());
			views.setCreateBy(loginUser.getUsername());
			return viewsMapper.insertViews(views);//新增浏览人
		} catch (DuplicateKeyException duplicateKeyException) {
			/*如果重复浏览*/
			loginUser = SecurityUtils.getLoginUser();
			LambdaQueryWrapper<Views> updateWrapper = new QueryWrapper<Views>().lambda()
					.eq(Views::getUserId, loginUser.getUserId())
					.eq(Views::getDataId, views.getDataId())
					.eq(Views::getType, views.getType());
			views.setUpdateTime(DateUtils.getNowDate());
			views.setUpdateBy(loginUser.getUsername());
			return viewsMapper.update(views, updateWrapper);//更新浏览时间
		} catch (ServiceException e) {
			log.info("当前浏览用户未登录");
			return 1;
		} finally {
			if (views.getType().equals("competition")) {
				/*比赛浏览*/
				Competition competition = competitionService.getById(views.getDataId());
				if (StringUtils.isNull(competition)) throw new ServiceException("比赛不存在");
				competition.setViews(competition.getViews() + 1);
				competitionService.updateById(competition);
			} else if (views.getType().equals("works")) {
				/*作品浏览*/
				Works works = worksService.getById(views.getDataId());
				if (StringUtils.isNull(works)) throw new ServiceException("作品不存在");
				works.setViews(works.getViews() + 1);
				worksService.updateById(works);
			}
		}
	}

	/**
	 * 修改浏览
	 *
	 * @param views 浏览
	 * @return 结果
	 */
	@Override
	public int updateViews(Views views) {
		views.setUpdateTime(DateUtils.getNowDate());
		views.setUpdateBy(SecurityUtils.getUsername());
		return viewsMapper.updateViews(views);
	}

	/**
	 * 批量删除浏览
	 *
	 * @param viewsIds 需要删除的浏览主键
	 * @return 结果
	 */
	@Override
	public int deleteViewsByViewsIds(Long[] viewsIds) {
		return viewsMapper.deleteViewsByViewsIds(viewsIds);
	}

	/**
	 * 删除浏览信息
	 *
	 * @param viewsId 浏览主键
	 * @return 结果
	 */
	@Override
	public int deleteViewsByViewsId(Long viewsId) {
		return viewsMapper.deleteViewsByViewsId(viewsId);
	}
}
